package com.controller;

import com.bean.anno.LogAnno;
import com.bean.base.TokenInfo;
import com.bean.base.UserInfo;
import com.bean.other.ResponseData;
import com.bean.wx.UserInfoWx;
import com.service.impl.UserInfoService;
import com.utils.JWTUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@CrossOrigin
@Slf4j
public class IndexController {

    @Autowired
    private JWTUtil jwtUtil;

    @Autowired
    private UserInfoService userInfoService;

//    @Autowired
//    private UserLogInfoService userLogInfoService;

    //@LogAnno(logType ="退出",details = "退出")
    @RequestMapping("/toLoginError")
    public ResponseEntity toLoginError(@RequestParam("error")String error){
        return ResponseEntity.ok(ResponseData.error(error));
    }

    @Value("${spring.web.resources.static-locations}")
    private String message;

    //登录
    @LogAnno(logType = "登录",details = "登录")
    @PostMapping("/login")
    public ResponseEntity loadLogin(@RequestBody UserInfo userInfo){
        log.error("静态资源访问路径");
        log.error("message："+message);
        userInfo=userInfoService.login(userInfo);
        if(userInfo==null){
            return ResponseEntity.ok(ResponseData.error("用户名或密码错误"));
        }else{
            String token = jwtUtil.generateToken(userInfo.getId());
            TokenInfo tokenInfo=new TokenInfo();
            tokenInfo.setName(userInfo.getUsername());
            List<String> roles =new ArrayList<String>();
            //roles.add("admin");//需要设置角色， 角色与菜单关联
            if(userInfo.getUsername().equals("admin")){
                roles.add("admin");
            }else{
                roles.add("editor");
            }
            tokenInfo.setRoles(roles);
            tokenInfo.setUserid(userInfo.getId());
            return ResponseEntity.ok(ResponseData.success("登录成功",token,tokenInfo));
        }

        /*String token= UUID.randomUUID().toString();
        LoginLog loginLog=new LoginLog();
        loginLog.setToken(token);
        loginLog.setUserId(userInfo.getId());
        loginLog.setUsername(userInfo.getUserName());
        loginLog.setDepartment(userInfo.getDepart_ment_name());
        loginLog.setUserType(userInfo.getUser_type());
        LoginUtil.addToken(loginLog);*/


        /*Cookie cookieUserId=new Cookie("id",userInfo.getId());
        cookieUserId.setPath("/");
        //cookieUserId.setDomain("/");

        Cookie cookieUserName=new Cookie("username",userInfo.getUsername());
        cookieUserName.setPath("/");*/
        //cookieUserName.setDomain("/");

        //response.addCookie(cookieUserId);
        //response.addCookie(cookieUserName);

       //因为跨域的问题导致不能使用session
        /*HttpSession httpSession=request.getSession();
        httpSession.setAttribute("id",userInfo.getId());
        httpSession.setAttribute("username",userInfo.getUserName());
        userInfo.setPassword(null);*/

        //return HttpRes.success("用户登录成功",token,userInfo);
    }


    //公众号登录登录
    @LogAnno(logType = "登录",details = "登录")
    @PostMapping("/wx/login")
    public ResponseEntity loadLogin(@RequestBody UserInfoWx userInfo){
        log.error("静态资源访问路径");
        log.error("message："+message);
        userInfo=userInfoService.login(userInfo);
        if(userInfo==null){
            return ResponseEntity.ok(ResponseData.error("用户名或密码错误"));
        }else{
            String token = jwtUtil.generateToken(userInfo.getId());
            TokenInfo tokenInfo=new TokenInfo();
            tokenInfo.setName(userInfo.getUsername());
            List<String> roles =new ArrayList<String>();
            //roles.add("admin");//需要设置角色， 角色与菜单关联
            if(userInfo.getUsername()==null){
                roles.add("editor");
            }else{
                if(userInfo.getUsername().equals("admin")){
                    roles.add("admin");
                }else{
                    roles.add("editor");
                }
            }

            tokenInfo.setRoles(roles);
            tokenInfo.setUserid(userInfo.getId());
            return ResponseEntity.ok(ResponseData.success("登录成功",token,tokenInfo));
        }
}


    //@LogAnno(logType ="退出",details = "用户退出")
    @GetMapping("/logout")
    public ResponseEntity logout() {
        // 清理会话数据
        /*session.removeAttribute("username");
        // ... 移除其他属性 ...

        // 或者使整个会话失效
        session.invalidate();*/
        //String token=request.getHeader("token");
        //LoginUtil.delLoginLog(token);
        //return HttpRes.success("注销成功");

        return ResponseEntity.ok(ResponseData.success("已退出"));
    }

}
